import { queryNavList, removeNav } from "@/api/content/navigation";
import { useState, useCallback, useEffect } from "react";
import { Navigation } from "../../../../types/content/navigation";
import { message } from "antd";
import { deleteFileInf } from "@/utils/deleteFileInf";

export const useNavigationList = () => {
    const [messageApi] = message.useMessage();
    const [loading, setLoading] = useState(false);
    const [searchForm, setSearchForm] = useState<Navigation.Query>({
        name: "",
        type: "",
    });
    const [tableData, setTableData] = useState<Navigation.Row[]>([]);
    /**
     * 获取标签列表
     */
    const getNavList = useCallback(async () => {
        setLoading(true);
        const res = await queryNavList(searchForm);
        setLoading(false);
        if (res.code === 200) {
            setTableData(res.data || []);
        }
    }, [searchForm]);

      /**
   * 删除文章
   * @param id 
   */
  const removeNavInf = async (row: Navigation.Row) => {
    setLoading(true)
    const res = await removeNav(row.id)
    setLoading(false)
    if (res.code === 200) {
      messageApi.success('删除成功')
      row.logo && deleteFileInf(row.logo)
      getNavList()
    }
  }
    useEffect(() => {
        getNavList();
    }, [getNavList, searchForm]);
    return {
        setSearchForm, 
        tableData, 
        loading,
        removeNavInf,
        getNavList
    }
}